package com.qingmeng.project.config;

import com.qingmeng.project.model.entity.InterfaceCountJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    private final Integer limit = 3;
    @Bean
    public JobDetail interfaceCountJobDetail() {
        return JobBuilder.newJob(InterfaceCountJob.class)
                .withIdentity("interfaceCountJob", "groupTest")
                .usingJobData("limit", limit)
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger interfaceCountTrigger() {
        return TriggerBuilder.newTrigger()
                .withIdentity("interfaceCountTrigger", "groupTest")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 2 * * ? "))
                .build();
    }

    @Bean
    public Scheduler scheduler(JobDetail interfaceCountJobDetail, Trigger interfaceCountTrigger) throws SchedulerException {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.scheduleJob(interfaceCountJobDetail, interfaceCountTrigger);
        scheduler.start();
        return scheduler;
    }
}
